#include "SoundEffects.h"

SoundEffects::SoundEffects(Ogre::String name){
	this->initNull();
	this->name = name;
}

SoundEffects::~SoundEffects(){
	if(this->overtimeTemporary != NULL)delete this->overtimeTemporary;
}

void SoundEffects::initNull(){

	this->overtime				= "";
	this->isStandard			=	false;	
	this->timeout				=	-1;
	
	this->overtime				=	false;
	this->overtimeTimer			=	-1;
	this->overtimeTemporary		=	NULL;

	//effects

	this->max_distance			=	1000;
	this->min_distance			=   1;
	
	this->pitch					=	1.0f;
	this->volume				=	1.0f;
	this->distance_effect_scale =	0.0f;
	this->doppler_effect_scale  =	1.0f;
	this->rolloff_scale			=   1.0f;

}

void SoundEffects::update(unsigned long millis){
	
	this->timeout = this->timeout - millis;

	if(this->overtimeTemporary != NULL){
		if(this->overtime){
			if(this->overtimeTimer <= 0){
				this->overtime = false;
			}
			else{
			this->updateOvertimeValues(millis);	
			}
		}
		else{
			
			this->synchronize(this->overtimeTemporary);
			delete this->overtimeTemporary;
			this->overtimeTemporary = NULL;

		}
	}
}

void SoundEffects::synchronize(SoundEffects *seffects){

	this->max_distance			=	seffects->max_distance;
	this->min_distance			=   seffects->min_distance;
	
	this->pitch					=	seffects->pitch;
	this->volume				=	seffects->volume;
	this->distance_effect_scale =	seffects->distance_effect_scale;
	this->doppler_effect_scale  =	seffects->doppler_effect_scale;
	this->rolloff_scale			=   seffects->rolloff_scale;

}

void SoundEffects::setOverTimeTemporary(SoundEffects effects, int time){

	if(this->overtimeTemporary != NULL){
		
		delete this->overtimeTemporary;
		this->overtimeTemporary = NULL;
		this->overtimeTimer = -1;

	}
	
	this->createOverTimeTemporary(effects,time);
	this->overtimeTimer = time;
	this->overtime = true;
}

void SoundEffects::updateOvertimeValues(unsigned long millis){

	this->max_distance			=	this->overtimeTemporary->max_distance			*millis;
	this->min_distance			=   this->overtimeTemporary->min_distance			*millis;
	
	this->pitch					=	this->overtimeTemporary->pitch					*millis;
	this->volume				=	this->overtimeTemporary->volume					*millis;
	this->distance_effect_scale =	this->overtimeTemporary->distance_effect_scale	*millis;
	this->doppler_effect_scale  =	this->overtimeTemporary->doppler_effect_scale	*millis;
	this->rolloff_scale			=   this->overtimeTemporary->rolloff_scale			*millis;

}

void SoundEffects::createOverTimeTemporary(SoundEffects effects, int time){
	if(time <= 0)return;

	this->overtimeTemporary = new SoundEffects("temp");
	
	this->overtimeTemporary->max_distance			=	effects.max_distance			/time;
	this->overtimeTemporary->min_distance			=   effects.min_distance			/time;
	
	this->overtimeTemporary->pitch					=	effects.pitch					/time;
	this->overtimeTemporary->volume					=	effects.volume					/time;
	this->overtimeTemporary->distance_effect_scale	=	effects.distance_effect_scale	/time;
	this->overtimeTemporary->doppler_effect_scale	=	effects.doppler_effect_scale	/time;
	this->overtimeTemporary->rolloff_scale			=   effects.rolloff_scale			/time;

}

const SoundEffects SoundEffects::operator +=(SoundEffects lhs){
	
	this->max_distance	= (this->max_distance + lhs.max_distance) /2;
	this->min_distance	= (this->min_distance + lhs.min_distance) /2;
	this->volume		= (this->volume + lhs.volume) /2;
	this->pitch			= (this->pitch + lhs.pitch) /2;
	this->rolloff_scale	= (this->rolloff_scale + lhs.rolloff_scale) /2;
	this->distance_effect_scale	= (this->distance_effect_scale + lhs.distance_effect_scale) /2;
	this->doppler_effect_scale	= (this->doppler_effect_scale + lhs.doppler_effect_scale) /2;

	return *this;
}

const SoundEffects operator+ (SoundEffects const lhs, SoundEffects const rhs){

	SoundEffects ret (lhs);
	
	ret += rhs;

	return ret;

}